import 'dart:async';
import 'package:fim/util/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

final StreamController<String?> selectNotificationStream =
    StreamController<String?>.broadcast();

void initNotification() async {
  flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('@mipmap/ic_launcher');
  final DarwinInitializationSettings initializationSettingsDarwin =
      DarwinInitializationSettings(
        requestSoundPermission: false,
        requestBadgePermission: false,
        requestAlertPermission: false,
      );

  InitializationSettings initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid,
    iOS:initializationSettingsDarwin
  );

  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onDidReceiveNotificationResponse: NotificationResp);
}

void NotificationResp(NotificationResponse notificationResponse) {
  switch (notificationResponse.notificationResponseType) {
    case NotificationResponseType.selectedNotification:
      selectNotificationStream.add(notificationResponse.payload);
      selectNotification(notificationResponse.payload);
      break;
    case NotificationResponseType.selectedNotificationAction:
    // if (notificationResponse.actionId == navigationActionId) {
    //   selectNotificationStream.add(notificationResponse.payload);
    // }
      break;
  }
}

Future selectNotification(String? payload) async {
  logger.i("selectNotification");
  if (payload != null) {
    debugPrint('notification payload: $payload');
  }
}

void showNotifications(String title, String body) async {
  const AndroidNotificationDetails androidPlatformChannelSpecifics =
      AndroidNotificationDetails(
          'your channel id','your channel name', channelDescription : 'your channel description',
          importance: Importance.max, priority: Priority.high, showWhen: false);
  const NotificationDetails platformChannelSpecifics =
      NotificationDetails(android: androidPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin
      .show(0, title, body, platformChannelSpecifics, payload: 'item x');
}
